/**
  ****************************(C) COPYRIGHT 2019 DJI****************************
  * @file       shoot.c/h
  * @brief      射击功能。
  * @note       
  * @history
  *  Version    Date            Author          Modification
  *  V1.0.0     Dec-26-2018     RM              1. 完成
  *
  @verbatim
  ==============================================================================

  ==============================================================================
  @endverbatim
  ****************************(C) COPYRIGHT 2019 DJI****************************
  */

#ifndef SHOOT_H
#define SHOOT_H
#include "struct_typedef.h"

#include "CAN_receive.h"
#include "gimbal_task.h"
#include "remote_control.h"
#include "user_lib.h"

extern float angle;
extern float set_angle;

#define SHOOT_TURN_BACK_KEY         KEY_PRESSED_OFFSET_G

//射击发射开关通道数据
#define SHOOT_RC_MODE_CHANNEL       1
//云台模式使用的开关通道

#define SHOOT_CONTROL_TIME          GIMBAL_CONTROL_TIME

#define SHOOT_FRIC_PWM_ADD_VALUE    100.0f

#define SHOOT_FRIC_MAX_SPEED_HIGH   6000.0f
#define SHOOT_FRIC_MAX_SPEED_LOW    6000.0f
#define SHOOT_FRIC_MIN_SPEED        0.0f

//射击摩擦轮激光打开 关闭
#define SHOOT_ON_KEYBOARD           KEY_PRESSED_OFFSET_Q
#define SHOOT_OFF_KEYBOARD          KEY_PRESSED_OFFSET_E

//射击完成后 子弹弹出去后，判断时间，以防误触发
#define SHOOT_DONE_KEY_OFF_TIME     15
//鼠标长按判断
#define PRESS_LONG_TIME             100
//遥控器射击开关打下档一段时间后 连续发射子弹
#define RC_S_LONG_TIME              400
#define RC_SHOOT_LONG_TIME					400
//视觉自动开火指令判断时间
#define AUTO_SHOOT_TIME             100
//摩擦轮高速 加速 时间
#define UP_ADD_TIME                 80
//电机反馈码盘值范围
#define HALF_ECD_RANGE              4096
#define ECD_RANGE                   8191
//电机rmp 变化成 旋转速度的比例
#define MOTOR_RPM_TO_SPEED          0.00290888208665721596153948461415f
#define MOTOR_ECD_TO_ANGLE          0.000021305288720633905968306772076277f
#define M3508_MOTOR_RPM_TO_VECTOR   0.000415809748903494517209f

#define FULL_COUNT                  18

//拨弹控制通道
#define SHOOT_CHANNEL   4       //

#define SHOOT_LEVEL			650			// 大于此临界值拨弹轮开始拨弹

//拨弹速度，分别是单发上限，连发，准备发射
#define TRIGGER_SPEED               -10.0f
#define CONTINUE_TRIGGER_SPEED      -10.0f
#define READY_TRIGGER_SPEED         0.0f

#define KEY_OFF_JUGUE_TIME          500
#define SWITCH_TRIGGER_ON           0
#define SWITCH_TRIGGER_OFF          1

//rocker value deadband 
//拨弹轮遥控死区
#define RC_DEADBAND   10

//卡单时间 以及反转时间
#define BLOCK_TRIGGER_SPEED         1.0f
#define BLOCK_TIME                  10
#define REVERSE_TIME                50
#define REVERSE_SPEED_LIMIT         13.0f
#define SHOOT_BULLET_TIME           20

#define PI_FOUR                     0.78539816339744830961566084581988f
#define PI_TEN                      0.314f
//#define PI_TEN                      0.00785f

//拨弹轮电机PID(角度环)
#define TRIGGER_ANGLE_PID_KP        80.0f
#define TRIGGER_ANGLE_PID_KI        0.005f
#define TRIGGER_ANGLE_PID_KD        0.0f

#define TRIGGER_BULLET_PID_MAX_OUT  10000.0f
#define TRIGGER_BULLET_PID_MAX_IOUT 9000.0f

#define TRIGGER_READY_PID_MAX_OUT   10000.0f
#define TRIGGER_READY_PID_MAX_IOUT  7000.0f

//拨弹轮电机PID（速度环）
#define TRIGGER_SPEED_PID_KP        800.0f
#define TRIGGER_SPEED_PID_KI        0.5f
#define TRIGGER_SPEED_PID_KD        0.0f

//#define TRIGGER_BULLET_PID_MAX_OUT  10000.0f
//#define TRIGGER_BULLET_PID_MAX_IOUT 9000.0f

#define TRIGGER_READY_PID_MAX_OUT   10000.0f
#define TRIGGER_READY_PID_MAX_IOUT  7000.0f

//摩擦轮电机PID
#define FRIC_SPEED_PID_KP           10.0f
#define FRIC_SPEED_PID_KI           0.1f
#define FRIC_SPEED_PID_KD           0.0f

#define FRIC_BULLET_PID_MAX_OUT     16000.0f
#define FRIC_BULLET_PID_MAX_IOUT    5000.0f

#define SHOOT_HEAT_REMAIN_VALUE     50
typedef enum
{
    SHOOT_STOP = 0,
    SHOOT_READY_FRIC,
    SHOOT_READY_BULLET,
    SHOOT_READY,
    SHOOT_BULLET,
    SHOOT_CONTINUE_BULLET,
    SHOOT_DONE,
		SHOOT_TURN_BACK,
    SHOOT_AUTO,
} shoot_mode_e;

typedef struct 
{
  uint8_t fric_error;
  uint8_t trigger_error;
}shoot_error_t;

typedef struct
{
    shoot_mode_e shoot_mode;
    const RC_ctrl_t *shoot_rc;
    const CV_Ctrl_t *shoot_cv;
    const motor_measure_t *shoot_motor_measure;
    const motor_measure_t *fric1_motor_measure;
    const motor_measure_t *fric2_motor_measure;
    ramp_function_source_t fric_ramp;
	
	  pid_type_def trigger_motor_pid_speed;
	  pid_type_def trigger_motor_pid_angle;
    pid_type_def fric1_motor_pid_speed;
    pid_type_def fric2_motor_pid_speed;
	
    first_order_filter_type_t fric1_filter;  //摩擦轮1滤波器
    first_order_filter_type_t fric2_filter;  //摩擦轮2滤波器

    fp32 trigger_speed_set;
	
    fp32 trigger_angle_set;
	
    fp32 speed;
    fp32 speed_set;
    fp64 angle;
    fp32 set_angle;
    int16_t trigger_given_current;
    int8_t ecd_count;

    bool_t press_l;
    bool_t press_r;
    bool_t last_press_l;
    bool_t last_press_r;
    uint16_t press_l_time;
    uint16_t press_r_time;
    uint16_t fire_cmd_time;
    uint16_t rc_s_time;
		
		uint16_t rc_SHOOT_time;
		
		uint16_t shoot_add_time;
		uint16_t stay_shoot_time;

    uint16_t block_time;
    uint16_t reverse_time;
    bool_t move_flag;
		bool_t turn_back_flag;

    bool_t key;
    uint8_t key_time;

    uint16_t heat_limit;
    uint16_t heat;

    fp32 fric1_speed;
    fp32 fric1_speed_set;
    fp64 fric1_angle;
    fp32 fric1_set_angle;
    int16_t fric1_given_current;
    int8_t fric1_ecd_count;

    fp32 fric2_speed;
    fp32 fric2_speed_set;
    fp64 fric2_angle;
    fp32 fric2_set_angle;
    int16_t fric2_given_current;
    int8_t fric2_ecd_count;

    uint16_t shoot_speed_limit;
    uint16_t remain_ammo;
    CV_Ctrl_t *cv_ctrl;
} shoot_control_t;
extern shoot_control_t shoot_control;
extern shoot_error_t shoot_error;
//由于射击和云台使用同一个can的id故也射击任务在云台任务中执行
extern void shoot_init(void);
extern int16_t  shoot_control_loop(void);

#endif
